import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
 * 
 *Gui of Review Management window for the user's use
 */
public class ReviewManagement implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JButton add = null;
	private JButton Edit = null;	
	private JLabel book=null;
	private JLabel bookExample=null;	
	private JButton remove = null;
	private JButton ViewReview = null;
	private JButton ViewAnother = null;
	public JTextArea reviewText = null;
	public JComboBox ReviewCombo = null;  
	private JButton cancel = null;  
	private JButton logout1 = null;
	public JPanel panel=new JPanel();

	/**
	 * This is the default constructor
	 */
	public ReviewManagement() {
		super();
		initialize();
	}
	
	/**
	 * This method Restes Gui Layout after Removal/Confirmation of a review
	 */
	public void ResetLayout()
	{
		ViewReview.setEnabled(true);
		Edit.setEnabled(false);
		add.setEnabled(false);
		remove.setEnabled(false);
		ViewAnother.setEnabled(false);
		reviewText.setText("");
		ReviewCombo.setEnabled(true);
		reviewText.setBackground(Color.LIGHT_GRAY);
		reviewText.setEditable(false);
		reviewText.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyTyped(java.awt.event.KeyEvent e) 
             {
                 Character c=e.getKeyChar();
                 if( c.charValue()==':' || c.charValue()=='*' || c.charValue()=='-' )
                 {
                     e.consume();
                     //Toolkit.getDefaultToolkit().beep();
                     JOptionPane.showMessageDialog(null,"Invalid key !!!!","ERROR",JOptionPane.ERROR_MESSAGE);
                     return;
                 }
             }	
		});
	}

	/**
	 * This method initializes add Review Management  Gui
	 * 
	 * @return void
	 */
	public void initialize() {
		
		book = new JLabel("Select Review:");
		book.setBounds(new Rectangle(20, 40, 240, 23));
		bookExample=new JLabel("Book name - username - (Review id)");
		bookExample.setFont(new Font("David",Font.ITALIC,14));
		bookExample.setForeground(Color.GRAY);
		bookExample.setBounds(new Rectangle(120, 21, 240, 23));
		
		panel.setLayout(null);
		panel.add(getAdd(), null);
		panel.add(getEdit(), null);		
		panel.add(getRemove(), null);
		panel.add(getViewReview(), null);
		panel.add(getViewAnother(), null);
		getReviewCombo();
		if (ReviewCombo!=null)
			panel.add(ReviewCombo,null);
		panel.add(getCancel(),null);
		panel.add(getLogout1(), null);
		panel.add(book);
		panel.add(bookExample);
		panel.add(getReviewText(), null);
		panel.setEnabled(false);
		panel.setVisible(false);
	}

	/**
	 * This method initializes ReviewText and set Key listener on this
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getReviewText() {
		if (reviewText == null) {
			reviewText = new JTextArea();
			reviewText.setBounds(new Rectangle(30, 140, 360, 230));
			reviewText.setBorder(BorderFactory.createLineBorder(Color.black));
			reviewText.setEditable(false);			
			reviewText.setBackground(Color.LIGHT_GRAY);	
			reviewText.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) 
	             {
	                 Character c=e.getKeyChar();
	                 if( c.charValue()==':' || c.charValue()=='*' || c.charValue()=='-' )
	                 {
	                     e.consume();
	                     //Toolkit.getDefaultToolkit().beep();
	                     JOptionPane.showMessageDialog(null,"Invalid key !!!!","ERROR",JOptionPane.ERROR_MESSAGE);
	                     return;
	                 }
	             }	
			});
		}
		return reviewText;
	}
	/**
	 * This method initializes ReviewCombo and gets Review List from DB to it
	 * 	
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getReviewCombo()
	{
		ReviewCombo=Main.revManagementCTRL.o_GetReviews();
		return ReviewCombo;
	}
	/**
	 * This method initializes ViewReview and set listener on this button
	 * 	
	 * @return javax.swing.JButton
	 */
	private JButton getViewReview() {
		if (ViewReview == null) {
			ViewReview = new JButton("View Selected Review");
			ViewReview.setBounds(new Rectangle(120, 70, 160, 41));
			ViewReview.addActionListener(this);
		}
		return ViewReview;
	}
		/**
	 * This method initializes ViewAnother and set listener on this button
	 * 	
	 * @return javax.swing.JButton
	 */
	private JButton getViewAnother() {
		if (ViewAnother == null) {
			ViewAnother = new JButton("View Another Review");
			ViewAnother.setBounds(new Rectangle(290, 70, 160, 41));
			ViewAnother.addActionListener(this);
			ViewAnother.setEnabled(false);
		}
		return ViewAnother;
	}
			/**
	 * This method initializes Edit and set listener on this button
	 * 	
	 * @return javax.swing.JButton
	 */
	private JButton getEdit() {
		if (Edit == null) {
			Edit = new JButton("Edit Review");
			Edit.setBounds(new Rectangle(420, 260, 140, 31));
			Edit.addActionListener(this);
			Edit.setEnabled(false);
		}
		return Edit;
	}
				/**
	 * This method initializes Add and set listener on this button
	 * 	
	 * @return javax.swing.JButton
	 */
	private JButton getAdd() {
		if (add == null) {
			add = new JButton("Confirm Review");
			add.setBounds(new Rectangle(420, 300, 140, 31));
			add.addActionListener(this);
			add.setEnabled(false);
		}
		return add;
	}
				/**
	 * This method initializes Cancel and set listener on this button
	 * 	
	 * @return javax.swing.JButton
	 */
	private JButton getCancel() {
		if (cancel == null) {
			cancel = new JButton("Back");
			cancel.setBounds(new Rectangle(406, 8, 74, 24));
			cancel.addActionListener(this);
		}
		return cancel;
	}
			/**
	 * This method initializes Logout and set listener on this button
	 * 	
	 * @return javax.swing.JButton
	 */
	JButton getLogout1() {
		if (logout1 == null) {
			logout1 = new JButton("Logout");
			logout1.setBounds(new Rectangle(490, 8, 74, 24));
			logout1.addActionListener(this);
		}
		return logout1;
	}
			/**
	 * This method initializes Remove and set listener on this button
	 * 	
	 * @return javax.swing.JButton
	 */
	private JButton getRemove() {
		if (remove == null) {
			remove = new JButton("Delete Review");
			remove.setBounds(new Rectangle(420, 340, 140, 31));
			remove.setEnabled(false);
			remove.addActionListener(this);
		}
		return remove;
	}
	
	public void actionPerformed(ActionEvent ae)
	{		
		if(ae.getSource()==ViewReview)
		{
			Main.revManagementCTRL.o_ViewReview(this);
			ViewReview.setEnabled(false);
			Edit.setEnabled(true);
			add.setEnabled(true);
			remove.setEnabled(true);
			ViewAnother.setEnabled(true);
			ReviewCombo.setEnabled(false);			
		}
		
		if(ae.getSource()==ViewAnother)
		{
			Main.revManagementCTRL.o_ViewReview(this);
			ResetLayout();
		}
				
		
		if(ae.getSource()==Edit)
		{
			reviewText.setEditable(true);
			reviewText.setBackground(Color.WHITE);
		}
		
		if(ae.getSource()==add)
		{
			Main.revManagementCTRL.o_ConfirmReview(this, "ConfirmReview");
		}
		
		if(ae.getSource()==remove)
		{
			Main.revManagementCTRL.o_ConfirmReview(this, "RemoveReview");
		}		
		
		if(ae.getSource()==cancel)			
			Main.revManagementCTRL.o_Back(this.panel);	
		
		if(ae.getSource()==logout1)				
			Main.revManagementCTRL.o_logout(this.panel);
	}	

}  
